package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {


    private IDepartmentService departmentService;

    @Autowired
    public void setDepartmentService(IDepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
//        department/list.jsp
        return "/department/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {
        System.out.println("id = " + id);
        if(id!=null){
        departmentService.delete(id);
        }
        return "redirect:/department/list";
    }

    @RequiredPermission(name="部门新增修改",expression = "Department.updateOtInsert")
    @RequestMapping("/updateOrInsert")
    public String updateOtInsert(Department department){
     if(department.getId()!=null){
                    departmentService.update(department);
     }
     else{
         departmentService.save(department);
     }

        return "redirect:/department/list";
    }
}
